﻿using System;
using System.Collections.Generic;
using System.Text;
using Vortex;
using Vortex.Drawing;

namespace Vortex.Tutorials.SpriteCollectionsAndAnimation {

    ///<summary>Demonstration of using </summary>
    class SpriteCollectionsAndAnimation : Game {
        Texture _SpritesheetTexture;

        SpriteGrid _RunSpriteGrid;
        ISpriteCollection _CustomCollection;
        SimpleSpriteAnimation _RunAnimation;

        Vector2 _ManPosition;

        protected override void Load () {
            _SpritesheetTexture = new Texture(@"{graphics}\runsheet.png");
            //Sprite grid can be created from texture, recture region or sprite with number of horizontal and vertical splits
            _RunSpriteGrid = new SpriteGrid(_SpritesheetTexture.ToSprite(), 7, 1);
            //Sprite list is just a wrapper for List<Sprite> which implements ISpriteCollection interface
            _CustomCollection = new SpriteList(new Sprite[] { _RunSpriteGrid[0], _RunSpriteGrid[3], _RunSpriteGrid[6]});
            //Sprite animation is created from ISpriteCollection interface and frame rate, it can be repeatable or not. All of properties can be adjusted runtime.
            _RunAnimation = new SimpleSpriteAnimation(_RunSpriteGrid, 20.0f);
        }

        protected override void Unload () {
            _SpritesheetTexture.Dispose();
        }

        protected override void Update (GameTime time) {
            //Sprite animation object should be updated each frame to calculate next frame
            _RunAnimation.Update(time.FrameTime);

            //Calculate position of running man
            _ManPosition = new Vector2(700 - (time.TotalGameTime * 500) % 800, 400);
        }

        protected override void Render (Canvas2D canvas) {
            canvas.Clear(ColorU.White);

            //Drawing all sprites from grid
            for (int n = 0; n < _RunSpriteGrid.Count; ++n ) {
                canvas.DrawSprite(new Vector2(32 + n * 64, 64), _RunSpriteGrid[n].Size * 0.33f, _RunSpriteGrid[n], ColorU.White);
            }

            //Drawing all sprites from custom composed sprite collection
            for (int n = 0; n < _CustomCollection.Count; ++n) {
                canvas.DrawSprite(new Vector2(480 + n * 64, 64), _CustomCollection[n].Size * 0.33f, _CustomCollection[n], ColorU.Red);
            }

            //Draw animated sprite, ToSprite() method returns sprite of current frame
            canvas.DrawSprite(320, 220, _RunAnimation.ToSprite().FlipVertical(), ColorU.White);

            //Drawing of running man with poor motion blur effect
            canvas.DrawSprite(_ManPosition + new Vector2(15, 0), _RunAnimation.CurrentSprite, ColorU.White.MultiplyAlpha(0.10f));
            canvas.DrawSprite(_ManPosition + new Vector2(10, 0), _RunAnimation.CurrentSprite, ColorU.White.MultiplyAlpha(0.30f));
            canvas.DrawSprite(_ManPosition + new Vector2(5, 0), _RunAnimation.CurrentSprite, ColorU.White.MultiplyAlpha(0.50f));
            canvas.DrawSprite(_ManPosition, _RunAnimation.CurrentSprite, ColorU.White);
        }
    }

}
